﻿using System;
using System.Reflection;
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using MugenInjection.Infrastructure;
using MugenInjection.Parameters;
using Should;
using TestModels.Class;
using System.Reflection;
using System.Linq;

namespace MugenInjectionTest.Parameters
{
    [TestClass]
    public class PropertyParameterTest
    {

        [TestMethod]
        public void CanResolveTest()
        {
#if NETFX_CORE
            var propertyString = typeof(ParameterTestModel).GetTypeInfo().DeclaredProperties.First(c => c.Name == "PublicPropertyString");
            var propertyInt = typeof(ParameterTestModel).GetTypeInfo().DeclaredProperties.First(c => c.Name == "PublicPropertyInt");
#else
            var propertyString = typeof(ParameterTestModel).GetProperty("PublicPropertyString");
            var propertyInt = typeof(ParameterTestModel).GetProperty("PublicPropertyInt");
#endif

            var parameter = new PropertyParameter("PublicPropertyInt", int.MaxValue);
            parameter.CanResolve(propertyString, null).ShouldBeFalse();
            parameter.CanResolve(propertyInt, null).ShouldBeTrue();

            parameter = new PropertyParameter("PublicPropertyInt", context => int.MaxValue);
            parameter.CanResolve(propertyString, null).ShouldBeFalse();
            parameter.CanResolve(propertyInt, null).ShouldBeTrue();

            parameter = new PropertyParameter("PublicPropertyInt", typeof(int), 0);
            parameter.CanResolve(propertyString, null).ShouldBeFalse();
            parameter.CanResolve(propertyInt, null).ShouldBeTrue();

            parameter = new PropertyParameter("PublicPropertyInt", context => string.Empty, typeof(int));
            parameter.CanResolve(propertyString, null).ShouldBeFalse();
            parameter.CanResolve(propertyInt, null).ShouldBeTrue();

            parameter = new PropertyParameter("PublicPropertyInt1", typeof(int), 0);
            parameter.CanResolve(propertyString, null).ShouldBeFalse();
            parameter.CanResolve(propertyInt, null).ShouldBeFalse();

            parameter = new PropertyParameter("PublicPropertyInt1", context => string.Empty, typeof(int));
            parameter.CanResolve(propertyString, null).ShouldBeFalse();
            parameter.CanResolve(propertyInt, null).ShouldBeFalse();

            parameter = new PropertyParameter("PublicPropertyInt", typeof(bool), 0);
            parameter.CanResolve(propertyString, null).ShouldBeFalse();
            parameter.CanResolve(propertyInt, null).ShouldBeFalse();

            parameter = new PropertyParameter("PublicPropertyInt", context => string.Empty, typeof(bool));
            parameter.CanResolve(propertyString, null).ShouldBeFalse();
            parameter.CanResolve(propertyInt, null).ShouldBeFalse();
        }

        [TestMethod]
        public void GetValueTest()
        {
            var value = new object();
            var value1 = new object();

            var parameter = new PropertyParameter("param1", value);
            parameter.GetValue(new ParameterContext(null, null)).ShouldEqual(value);

            parameter = new PropertyParameter("param1", context => value1);
            parameter.GetValue(new ParameterContext(null, null)).ShouldEqual(value1);
        }
    }
}